package com.poobo.util.date;



import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.persistence.Entity;

import com.poobo.util.str.StrUtil;

/**
 * 日期工具类
 * @author xiayy
 * @Created on data 2015-06-22
 */
@Entity
public class DateUtil
{

	public DateUtil(){
	}

	// 获取系统当前时间
	public static Timestamp getNowTime()
	{
		return new Timestamp(System.currentTimeMillis());
	}

	// 获取年 格式(YY)
	public static String getYear()
	{
		SimpleDateFormat date = new SimpleDateFormat("yy");
		return date.format(new Date());
	}

	// 获取年 格式(YYYY)
	public static String getYearYYYY()
	{
		SimpleDateFormat date = new SimpleDateFormat("yyyy");
		return date.format(new Date());
	}
	
	// 获取年 格式(yyyy-MM-dd hh:mm:ss)
	public static String formatYYmmddhhmmss(Date dates)
	{
			SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			return date.format(dates);
	}

	// 获取月份 格式(MM)
	public static String getMonth()
	{
		SimpleDateFormat date = new SimpleDateFormat("MM");
		return date.format(new Date());
	}

	// 获取日 格式(dd)
	public static String getDay()
	{
		SimpleDateFormat date = new SimpleDateFormat("dd");
		return date.format(new Date());
	}

	// 获取时 格式(HH)
	public static String getHour()
	{
		SimpleDateFormat date = new SimpleDateFormat("HH");
		return date.format(new Date());
	}

	// 获取分 格式(mm)
	public static String getMinute()
	{
		SimpleDateFormat date = new SimpleDateFormat("mm");
		return date.format(new Date());
	}

	// 获取秒 格式(ss)
	public static String getSecond()
	{
		SimpleDateFormat date = new SimpleDateFormat("ss");
		return date.format(new Date());
	}

	// 获取毫秒 格式(sss)
	public static String getMillisecond()
	{
		SimpleDateFormat date = new SimpleDateFormat("SSS");
		return date.format(new Date());
	}

	// 获取时分秒 格式(yy-MM-dd)
	public static boolean isToday(Timestamp time)
	{
		SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
		return format.format(time).equals(format.format(new Date()));
	}

	// 根据格式字符串转换时间
	public static String getCurrentDay(String dateFormat)
	{
		SimpleDateFormat format = new SimpleDateFormat(dateFormat);
		return format.format(new Date());
	}

	// 获取时间差
	public static long getDayBetween(String now, String fore)
	{
		long count = 0L;
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		try
		{
			Date nowDate = myFormatter.parse(now);
			Date foreDate = myFormatter.parse(fore);
			count = (nowDate.getTime() - foreDate.getTime()) / 0x5265c00L;
		}
		catch (ParseException e)
		{
			count = 0L;
			e.printStackTrace();
		}
		return count;
	}
	

	// 创建当前时间的戳值
	public static Timestamp getTodayTimestamp()
	{
		String nowDateStr = getCurrentDay("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Timestamp timestamp = null;
		try
		{
			timestamp = new Timestamp(sdf.parse(nowDateStr).getTime());
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		return timestamp;
	}
	
	
	public static Timestamp rollDayTimestamp(String dateStr, int amount)
	{
		String nowDate = dateStr;
		if (nowDate == null || nowDate.trim().equals(""))
			nowDate = getCurrentDay("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar nowCalendar = Calendar.getInstance();
		try
		{
			nowCalendar.setTime(sdf.parse(nowDate));
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		nowCalendar.add(5, amount);
		Timestamp timestamp = new Timestamp(nowCalendar.getTimeInMillis());
		return timestamp;
	}

	public static Timestamp rollMonthTimestamp(String dateStr, int amount)
	{
		String nowDate = dateStr;
		if (nowDate == null || nowDate.trim().equals(""))
			nowDate = getCurrentDay("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar nowCalendar = Calendar.getInstance();
		try
		{
			nowCalendar.setTime(sdf.parse(nowDate));
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		nowCalendar.add(2, amount);
		Timestamp timestamp = new Timestamp(nowCalendar.getTimeInMillis());
		return timestamp;
	}

	// 戳值转换成时间
	public static String format(Timestamp time, String formatStr)
	{
		if (time == null)
		{
			return null;
		} else
		{
			SimpleDateFormat format = new SimpleDateFormat(formatStr);
			String date = format.format(time);
			return date;
		}
	}

	public static String format(String time, String formatStr)
	{
		if (StrUtil.isNull(time))
			return null;
		String date = "";
		try
		{
			SimpleDateFormat format = new SimpleDateFormat(formatStr);
			date = format.format(new Timestamp((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(time).getTime()));
		}
		catch (ParseException e)
		{
			e.printStackTrace();
			return "";
		}
		return date;
	}

	// 月份转换成中文
	public static String getMonthStr(int month)
	{
		String monthStr = "";
		switch (month)
		{
		case 1: // '\001'
			monthStr = "一月";
			break;

		case 2: // '\002'
			monthStr = "二月";
			break;

		case 3: // '\003'
			monthStr = "三月";
			break;

		case 4: // '\004'
			monthStr = "四月";
			break;

		case 5: // '\005'
			monthStr = "五月";
			break;

		case 6: // '\006'
			monthStr = "六月";
			break;

		case 7: // '\007'
			monthStr = "七月";
			break;

		case 8: // '\b'
			monthStr = "八月";
			break;

		case 9: // '\t'
			monthStr = "九月";
			break;

		case 10: // '\n'
			monthStr = "十月";
			break;

		case 11: // '\013'
			monthStr = "十一月";
			break;

		case 12: // '\f'
			monthStr = "十二月";
			break;
		}
		return monthStr;
	}

	public static int getWeekOfYear()
	{
		int week = 0;
		Calendar c = Calendar.getInstance();
		week = c.get(3);
		return week;
	}

	public static String getStartDate(int year, int weekNo)
	{
		Calendar cal = Calendar.getInstance();
		cal.set(1, year);
		cal.set(3, weekNo);
		cal.set(7, 2);
		SimpleDateFormat returnDate = new SimpleDateFormat("yyyy年MM月dd日");
		return returnDate.format(cal.getTime());
	}

	public static String getEndDate(int year, int weekNo)
	{
		Calendar cal = Calendar.getInstance();
		cal.set(1, year);
		cal.set(3, weekNo + 1);
		cal.set(7, 1);
		SimpleDateFormat returnDate = new SimpleDateFormat("yyyy年MM月dd日");
		return returnDate.format(cal.getTime());
	}

	public static Collection getTwoDateDayList(String endDate, String beginDate)
	{
		Collection resultList = new ArrayList();
		long dayCount = getDayBetween(endDate, beginDate);
		resultList.add(beginDate);
		for (int i = 0; (long)i < dayCount; i++)
		{
			Timestamp now = rollDayTimestamp(beginDate, 1);
			beginDate = format(now, "yyyy-MM-dd");
			resultList.add(beginDate);
		}

		return resultList;
	}

	public static Collection getTwoDateDayListDesc(String endDate, String beginDate)
	{
		Collection resultList = new ArrayList();
		long dayCount = getDayBetween(endDate, beginDate);
		resultList.add(endDate);
		for (long i = dayCount; i > 0L; i--)
		{
			Timestamp now = rollDayTimestamp(endDate, -1);
			endDate = format(now, "yyyy-MM-dd");
			resultList.add(endDate);
		}

		return resultList;
	}

	public static String getWeekForDate(String date, String format)
	{
		String week = "";
		SimpleDateFormat f1 = new SimpleDateFormat(format);
		Date d = null;
		try
		{
			d = f1.parse(date);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		SimpleDateFormat formatter = new SimpleDateFormat("E");
		week = formatter.format(d);
		return week;
	}

	public static String getStandardDate(int year, int month, int day)
	{
		String yearStr = String.valueOf(year);
		String monthStr = String.valueOf(month);
		String dayStr = String.valueOf(day);
		if (month < 10)
			monthStr = (new StringBuilder("0")).append(month).toString();
		if (day < 10)
			dayStr = (new StringBuilder("0")).append(day).toString();
		return (new StringBuilder(String.valueOf(yearStr))).append("-").append(monthStr).append("-").append(dayStr).toString();
	}

	public static String rollHoursTimestamp(String dateStr, String amount)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(dateStr);
		String timeStr = getCurrentDay(dateStr);
		Date time = null;
		try
		{
			time = sdf.parse(timeStr);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		int hours = StrUtil.getNotNullIntValue(amount, 0);
		Date torollHours = new Date(time.getTime() - (long)(hours * 3600 * 1000));
		return sdf.format(torollHours).toString();
	}

	public static boolean compareTotime(String dateStr)
	{
		boolean isOK = false;
		long count = 0L;
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowtimeStr = getCurrentDay("yyyy-MM-dd HH:mm:ss");
		try
		{
			Date nowDate = myFormatter.parse(nowtimeStr);
			Date foreDate = myFormatter.parse(dateStr);
			count = nowDate.getTime() - foreDate.getTime();
		}
		catch (ParseException e)
		{
			count = 0L;
			e.printStackTrace();
		}
		if (count > 0L)
			isOK = true;
		return isOK;
	}

	public static boolean compareTotime(String oneStr, String twoStr)
	{
		boolean isOK = false;
		long count = 0L;
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try
		{
			Date nowDate = myFormatter.parse(oneStr);
			Date foreDate = myFormatter.parse(twoStr);
			count = nowDate.getTime() - foreDate.getTime();
		}
		catch (ParseException e)
		{
			count = 0L;
			e.printStackTrace();
		}
		if (count > 0L)
			isOK = true;
		return isOK;
	}

	public static Timestamp getNotNullTimestampValue(String data, String dateFormat)
	{
		Timestamp value;
		try
		{
			if (data == null || data.equals(""))
			{
				value = new Timestamp(System.currentTimeMillis());
			} else
			{
				SimpleDateFormat smd = new SimpleDateFormat(dateFormat);
				value = new Timestamp(smd.parse(data).getTime());
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			value = new Timestamp(System.currentTimeMillis());
		}
		return value;
	}

	public static int compareTimstampStr(String sourceStr, String destStr, String formartStr)
	{
		int result = 10;
		SimpleDateFormat dateFormatter = new SimpleDateFormat(formartStr);
		long temp = 10L;
		try
		{
			Date beginDate = dateFormatter.parse(sourceStr);
			Date endDate = dateFormatter.parse(destStr);
			temp = beginDate.getTime() - endDate.getTime();
		}
		catch (ParseException e)
		{
			result = 0x7fffffff;
			e.printStackTrace();
		}
		if (temp > 0L)
			result = 1;
		else
		if (temp == 0L)
			result = 0;
		else
		if (temp < 0L)
			result = -1;
		return result;
	}

	public static String getDateFormat(String date)
	{
		Timestamp nowDay = getTodayTimestamp();
		String hour = date.substring(11, date.length());
		date = format(date, "yyyy-MM-dd");
		if (date.equals(getCurrentDay("yyyy-MM-dd")))
			return hour;
		if (nowDay.equals(rollDayTimestamp(date, 1)))
			return (new StringBuilder("昨天 ")).append(hour).toString();
		if (nowDay.equals(rollDayTimestamp(date, 2)))
			return (new StringBuilder("前天 ")).append(hour).toString();
		else
			return (new StringBuilder(String.valueOf(date))).append(" ").append(hour).toString();
	}
	
	/**
	 * 将当前时间格式化为指定字符串
	 * 
	 * @param pattern
	 *            eg:yyyy-MM-dd
	 * @return
	 */
	public static String getNowTimeStr(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(pattern);
		return sdf.format(new Date());
	}

	/**
	 * 将当前时间格式化为指定字符串
	 * 
	 * @param pattern
	 *            eg:yyyy-MM-dd
	 * @return
	 */
	public static String getTimeStr(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(pattern);
		return sdf.format(date);
	}

	/**
	 * 当前时间天数波动值
	 * 
	 * @param value
	 * @return
	 */
	public static String getNowDateDayFloat(String pattern, int value) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, value);
		return getTimeStr(calendar.getTime(), pattern);
	}

	public static Date parseDateStr(String str, String pattern) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat();
			sdf.applyPattern(pattern);
			return sdf.parse(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取某个时间的当月最后一刻
	 * @param str
	 * @param pattern
	 * @return
	 */
	public static Date lastMomentOfMonth(String str, String pattern){
		Date date = parseDateStr(str, pattern);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.SECOND, -1);
		return calendar.getTime();
	}
	
	/**
	 * 查询所给时间所在月份的第一天是周几
	 * @param date   yyyy年MM月
	 * @return
	 */
	public static int firstDayIndexOfWeek(String time,String format){
		Date date = DateUtil.parseDateStr(time, format);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}
	
	/**
	 * 查询某月的天数
	 * @param time
	 * @param format
	 * @return
	 */
	public static int DaysOfMonth(String time,String format){
		Date date = DateUtil.parseDateStr(time, format);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
}
